import java.applet.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class Brush1 extends Applet
   implements MouseMotionListener {

   int width = 250;
   int height = 250;
   Image backbuffer;
   Graphics backg;
   Color color;
   static int size;
   
//Code to initialize the drawing area.
   public void init() 
   {
	  //Creates the background area. 
 	  backbuffer = createImage(width,height);
 	  
 	  //Makes the background white.
      backg = backbuffer.getGraphics();
      backg.setColor( Color.white );
      backg.fillRect( 0, 0, width, height );
      
      //Sets the variable globals to their default values.
      color = Color.white;
      size = 10;
      
      //Adds a Mouse Motion Listener to the frame.
      addMouseMotionListener( this ); 
   }

   //Code to run when mouse is dragged over frame.
   public void mouseDragged( MouseEvent e ) {
	   //Get coordinates of mouse drag
      int x = e.getX();
      int y = e.getY();
      
      //Fill an oval of the proper size setting with the current color.
      backg.fillOval(x-5,y-5,size,size);
      
      //repaints the image to the screen and deletes MouseEvent e.
      repaint();
      e.consume();
   }

   //Frame required code for update.
   public void update( Graphics g ) {
      g.drawImage( backbuffer, 0, 0, this );
   }

   //Code to paint the frame.
   public void paint( Graphics g ) {
      update( g );
   }
   
 
 //Code to refresh the drawing frame to all white while preserving the current color and size settings.  
 public void refresh()
  {
	  backbuffer = createImage(width,height);
				backg = backbuffer.getGraphics();
				backg.setColor( Color.white );
			    backg.fillRect( 0, 0, width, height );
			    backg.setColor(color);
			    repaint();
  }

 //Code to change the color that is placed in the drawing area.
public void changeColor(String string) 
{
	if(string.equalsIgnoreCase("blue"))
	{
		color = Color.blue;
	}
	else if(string.equalsIgnoreCase("red"))
	{
		color = Color.RED;
	}
	else if(string.equalsIgnoreCase("green"))
	{
		color = Color.GREEN;
	}
	else if(string.equalsIgnoreCase("orange"))
	{
		color = Color.ORANGE;
	}
	else if(string.equalsIgnoreCase("yellow"))
	{
		color = Color.YELLOW;
	}
	else if(string.equalsIgnoreCase("white"))
	{
		color = Color.WHITE;
	}
	backg.setColor(color);
}

//Changes the Size of the drawn dot for each mouse drag.
public void changeSize(int i) 
{
	size = i;
}

//Background getters and setters.
public Image getBackbuffer() {
	return backbuffer;
}

public void setBackbuffer(Image backbuffer) {
	this.backbuffer = backbuffer;
}

@Override
public void mouseMoved(MouseEvent arg0) {
	// TODO Auto-generated method stub
	
}

}
